/**
 * Created by john on 2016/8/31.
 */
(function () {
    'use strict';

    angular
        .module('user.nick.name.controller', [])
        .controller('NickNameCtrl',NickNameCtrl);

    NickNameCtrl.$inject = ['$scope','$yikeUtils','$state','$ionicHistory','$ionicModal','$ionicLoading','localStorageService'];
    /* @ngInject */
    function NickNameCtrl($scope,$yikeUtils,$state,$ionicHistory,$ionicModal,$ionicLoading,localStorageService) {
      var user = localStorageService.get("user");
      $scope.confirm = confirm;
      $scope.user = {
          oldName:user.nickname || "还未有昵称",
          newName:""
      };
      function confirm(){
          if(!$scope.user.newName){
              $yikeUtils.toast("请先输入新昵称");
          }else{
              $ionicLoading.show({
                  template: '<ion-spinner icon="bubbles"></ion-spinner>'
              });
              yikeTaishan.reviseName(user.token,$scope.user.newName)
                  .then(function(data){
                      $ionicLoading.hide();
                      if(data.status == 1){
                          user.nickname = $scope.user.newName;
                          localStorageService.set("user",user);
                          $scope.user.newName = "";
                          $state.go("tab.account");
                      }else{
                          $yikeUtils.toast(data.result.collection);
                      }
                  });
          }
      }
    }
})();
